For element
For Element
Overview
The For element offers the possibility to include a cycle to be executed a specified number of times. The initial value of a variable will increase by a set amount on each iteration. The cycle will be executed as long as a Loop Condition is met.
The For window has four requirements:
- Loop Initialization: Indicates the loop control variable used to regulate the number of times the loop is executed.
- Initial Value: The initial value of the loop control variable is set equal to a constant value. This value can be any number that fulfills your purpose; the default is zero (0).
- Loop Increase / Decrease: Indicates the amount by which the variable will increase or decrease in each iteration. The default is an increment of one (1).
- Loop Condition: Evaluated at the end of every loop. The loop continues to iterate while the condition is true. If the condition is false, the loop finishes, and the workflow continues to the next element on the Activity diagram.
What You Need to Do in Bizagi
- Create a scripting expression.
- Declare a variable to store all the elements of the collection.
- Create a For element to include the logic of the cycle until a condition is true.
- Within the For loop, establish its characteristics to perform the required calculations.
- Save the rule.
Example
Suppose you work in a bank where you perform an initial appraisal of the applicant to see if they are eligible for the financial products requested.
If the applicant passes this first assessment, the bank will request several documents from the customer to support the data presented. There is a parameter entity, Applicant Document, that contains a list of all documents the bank may require based on the products requested. Therefore, it is necessary to examine the list and extract only the documents that apply. These documents will be related to a Table in the Case.
The following is the Data Model of the example, where parameter entities are in green and master entities in blue:
Steps
1. Create a rule On Enter of the Activity where the document list will be displayed.
2. Declare the variables to be used:
- DocsXApplicant (object): The loop iteration variable storing the specific documents selected from the parameter table.
- Count (int): The looping variable. It starts at zero and increments by one for each loop.
- IdDocument (int): A variable storing the unique identifier of the Document, pointing to the current record in the DocsXApplicant collection.
- ApplicantDocument (object): A result variable that stores the selected documents to be added to the Case's collection.
3. Include an expression element to obtain the records of the Document per Applicant parameter entity that apply to the Case.
// Build the filter according to the applicant's role and the income source
var parameters = new FilterParameters();
parameters.AddParameter("@idApplicantRole", <idApplicant.idApplicantRole.id>);
parameters.AddParameter("@IncomeSource", <idApplicant.IncomeSource.id>);
// Store the records of the Parameter entity that comply with the filter in a variable
DocsXApplicant = Me.getXPath("entity-list('DocumentXApplicant', 'idApplicantRole = @idApplicantRole and IncomeSource = @IncomeSource')", parameters);
4. Include the For element.
- Select the declared variable and initialize it to zero, so the starting point is the first record.The variable will increment by one, adding each record of DocsXApplicant to the ApplicantDocument collection.
Create the condition: The loop will execute as long as the DocsXApplicant variable contains records to add to the collection.
Count < DocsXApplicant.size();
5. Include an assignment element to add the records to the collection of documents.
// Store the Document identifier that points to the current record in DocsXApplicant collection (loop iteration variable).
IdDocument = DocsXApplicant[Count].getXPath("idDocument");
// Add an element (a new record) to the Applicant Document entity (one-to-many relationship).
ApplicantDocument = Me.newCollectionItem("idApplicant.Documents");
// Set the Document identifier of the newly created record.
ApplicantDocument.setXPath("idDocument", IdDocument);
// Set the Required attribute (True or False) of the newly created record.
ApplicantDocument.setXPath(
"Required",
DocsXApplicant[Count].getXPath("Required")
);
Note: The variable ApplicantDocument is used to copy values from the Document per Applicant parameter entity to the Applicant Document entity. The expression adds each record individually, iterating through the parameter entity.
Variable.setXPath("attribute within the collection", value to set);